package com.zjitc.net.homework3.bind;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.zjitc.net.homework3.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    protected static final String TAG = "MainActivity";

    Button btnOpen, btnBind, btnClose;

    private ServiceConnection coon;
    private MyService myService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnOpen = (Button) findViewById(R.id.btn_open);
        btnBind = (Button) findViewById(R.id.btn_bind);
        btnClose = (Button) findViewById(R.id.btn_close);

        btnOpen.setOnClickListener(this);
        btnBind.setOnClickListener(this);
        btnClose.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final Intent intent = new Intent(this, MyService.class);
        switch (v.getId()) {
            case R.id.btn_open:
                startService(intent);
                break;
            case R.id.btn_bind:
                // 开启绑定
                btnOpen.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d(TAG, "绑定调用：bindService");

                        //调用绑定方法
                        bindService(intent, coon, Service.BIND_AUTO_CREATE);

                    }
                });
                // 解除绑定
                btnClose.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d(TAG, "解除绑定调用：unbindService");

                        //解除绑定
                        if (myService != null) {
                            myService = null;
                            unbindService(coon);
                        }

                    }
                });
                // 获取数据
                btnBind.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (myService != null) {
                            // 通过绑定服务传递的Binder对象，获取Service暴露出来的数据

                            Log.d(TAG, "从服务端获取数据：" + myService.getCount());
                        } else {

                            Log.d(TAG, "还没绑定呢，先绑定,无法从服务端获取数据");
                        }
                    }
                });
                coon = new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        Log.d(TAG, "绑定成功调用：onServiceConnected");
                        // 获取Binder

                    }

                    @Override
                    public void onServiceDisconnected(ComponentName name) {

                    }
                };
                break;
            case R.id.btn_close:
                stopService(intent);
                break;
        }
    }
}
